package com.jpress.ejb;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Stateless;

import org.apache.commons.mail.EmailException;

import com.jpress.model.Usuario;
import com.jpress.remote.UsuarioRemote;
import com.jpress.utils.EmailUtils;

@Stateless(mappedName = "bean/aniversario")
@Singleton
@Startup
public class AniversarioTimerService {

	@EJB
	private UsuarioRemote usuarioRemote;
	List<Usuario> aniversariantes;

	@Schedule(hour="16", minute="30", second="0",dayOfWeek="*", persistent = false)
	public void run() {
		Calendar hoje = new GregorianCalendar();
		Integer dia = hoje.get(Calendar.DAY_OF_MONTH);
		Integer mes = hoje.get(Calendar.MONTH) + 1;

		aniversariantes = usuarioRemote.findByDataAniversario(dia, mes);

		for (Usuario u : aniversariantes) {

			ArrayList<String> dest = new ArrayList<String>();
			dest.add(u.getEmail());
			String msg = "Parabens \""
					+ u.getNome()
					+ "\" desejamos muita paz e felicidades nesse dia especial!";
			try {
				EmailUtils.enviarEmailTexto("Parabens!", msg, dest, null, null);
			} catch (EmailException e) {
				e.printStackTrace();
			}
		}
	}

}
